﻿#include "scripttemplate.h"
#include "world.h"

void CScriptXMLParser::startElement(void *ctx, const char *name, const char **atts)
{
	_lastelement = (char*)name;
}

void CScriptXMLParser::endElement(void *ctx, const char *name)
{
	_lastelement = "";
}

void CScriptXMLParser::textHandler(void *ctx, const char *ch, int len)
{
	string value = std::string((char*)ch,0,len);

	if(_lastelement == "id")
	{
		_lastTemplate = new TEMPLATE_TYPE();
		_lastTemplate->id = atoi(value.c_str());

		_world->InsertScriptObject(_lastTemplate);
	}
	else if(_lastelement == "characterid")
		_lastTemplate->characterid = atoi(value.c_str());
	else if(_lastelement == "npc_name")
		_lastTemplate->npc_name = value;
	else if(_lastelement == "begin_gen_time")
		_lastTemplate->begin_gen_time = atoi(value.c_str());
	else if(_lastelement == "repeat_gen_time")
		_lastTemplate->repeat_gen_time = atoi(value.c_str());
	else if(_lastelement == "team")
		_lastTemplate->team = atoi(value.c_str());
	else if(_lastelement == "hp")
		_lastTemplate->hp = atoi(value.c_str());
	else if(_lastelement == "att")
		_lastTemplate->att = atoi(value.c_str());
	else if(_lastelement == "def")
		_lastTemplate->def = atoi(value.c_str());
	else if(_lastelement == "genpos")
		_lastTemplate->genpos = value;
	else if(_lastelement == "goalpos")
		_lastTemplate->goalpos = value;
}

bool CScriptXMLParser::initParsing(CWorld* world)
{
	_world = world;

	_animFile = "map/script_map1.xml";

	CCSAXParser parser;

	if (false == parser.init("UTF-8"))
	{
		return false;
	}
	parser.setDelegator(this);

	const char *xmlPath = CCFileUtils::fullPathFromRelativePath(_animFile);
	parser.parse(xmlPath);
	return true;
}
